<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/View.php');

Class FormView extends BaseView
{
	function FormView()
	{
		parent::BaseView();
	}
	
	function ShowForm(&$form, $id)
	{
		$this->output = '<h2>'.htmlspecialchars($form->Collection[0]->Title).'</h2>';

		$this->output .= '<form method="post" action="'.$_SERVER['SCRIPT_NAME'].'">';
		$this->output .= '<input type="hidden" id="formID" name="formID" value="'.$id.'" />';
		$this->output .= '<input type="hidden" id="formCode" name="formCode" value="'.md5(uniqid(rand(), true)).'" />';
		$this->output .= '<input type="text" class="antiLeech" id="antiLeech" name="antiLeech" value="" />';
		$this->output .= '<table class="form">';
		
//print('<pre>'.var_export($form,true).'</pre>');exit;
		foreach($form->Collection[0]->FormFields->Collection as $key=>$formField)
		{
			if ($formField->SortOrder >= 0)
			{
				$id = 'ff_'.$form->Collection[0]->ID.'_'.$formField->Name;
				
				$this->output .= '<tr>';
				$this->output .= '<td class="formFieldLabel"><label for="'.$id.'">'.$formField->Label.'</label></td>';
				$this->output .= '<td class="formField">';
				switch ($formField->Type)
				{
					case TEXTINPUT: // TEXTINPUT
						$this->output .= $this->RenderTextInput($formField, $id);
						break;

					case CHECKBOX: // CHECKBOX
						$this->output .= $this->RenderCheckBox($formField, $id);
						break;

					case TEXTAREA: // TEXTAREA
						$this->output .= $this->RenderTextArea($formField, $id);
						break;
				}
				$this->output .= '</td>';
				$this->output .= '</tr>';
			}
		}
		$this->output .= '<tr>';
		$this->output .= '<td class="formFieldLabel">&nbsp;</td>';
		$this->output .= '<td class="formField">';
		$this->output .= '<input type="submit" class="formFieldEntry formFieldButton" id="btnSubmit" name="btnSubmit" value="'.$form->Collection[0]->Submit.'" />';
		$this->output .= '</td>';
		$this->output .= '</tr>';

		$this->output .= '</table>';
		$this->output .= '</form>';
	}
	
	function ShowFeedback($form)
	{
		$this->output  = '<h2>'.htmlspecialchars($form->Title).'</h2>';
		$this->output .= $form->Message;
	}
	
	function ShowError($form)
	{
		//$this->output  = '<h2>'.$form->Title.'</h2>';
		$this->output .= '<div class="formWarning">';
		$this->output .= '<p>';
		$this->output .= count($form->Errors) == 1 ? 'Er is een fout opgetreden:' : 'Er zijn fouten opgetreden:';
		$this->output .= '</p>';
		$this->output .= '<ul>';
		foreach ($form->Errors as $error)
		{
			$this->output .= '<li>' . $error . '</li>';
		}
		$this->output .= '</ul>';
		$this->output .= '<p>';
		$this->output .= 'Gelieve deze te verbeteren.';
		$this->output .= '</p>';
		$this->output .= '</div>';
	}
	
	/* Control Rendering */
	function RenderTextInput($formField, $id)
	{
		$output  = '';

		$output .= '<input type="text" class="formFieldEntry" id="'.$id.'" name="'.$id.'" ';
		$output .= 'maxlength="'.$formField->Length.'" ';
		if ($formField->Length < 100)
		{
			$output .= 'size="'.floor($formField->Length/2).'" ';
		}
		$output .= 'value="'.$formField->Value.'" ';
		$output .= '/>';
		
		return $output;
	}
	
	function RenderCheckBox($formField, $id)
	{
		$output  = '';
		
		$output .= '<input type="checkbox" class="formFieldEntry" id="'.$id.'" name="'.$id.'" value="" />';
		
		return $output;
	}
	
	function RenderTextArea($formField, $id)
	{
		$output  = '';

		$output .= '<textarea class="formFieldEntry" id="'.$id.'" name="'.$id.'" ';
		$output .= 'cols="100" ';
		$output .= 'rows="'.floor($formField->Length/100).'" ';
		//$output .= 'maxlength="'.$formField->Length.'" ';
		//if ($formField->Length < 100)
		//{
		//	$output .= 'size="'.floor($formField->Length/1.5).'" ';
		//}
		$output .= '>';
		$output .= $formField->Value;
		$output .= '</textarea>';
		
		return $output;
	}
	
}
?>